﻿Imports System.Web
Imports System.Web.Services
Imports MeoTom.Core.BLL
Imports MeoTom.Core

Public Class ValidateArticle
    Implements System.Web.IHttpHandler

    Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        context.Response.ContentType = "text/plain"
        Dim year As String = ""
        Dim month As String = ""
        Dim day As String = ""
        Dim ArticleID As String = ""
        Dim i As Integer = 0
        Dim a As New Articles()
        Dim util As New Utilities.Common()
        Try
            ArticleID = util.IntValue(context.Request.QueryString("ArticleID").Replace(".aspx", "")).ToString()
            Dim strUrl As String = context.Request.QueryString("sPath") & "-" & context.Request.QueryString("ArticleID")
            For Each str As String In Regex.Split(strUrl, "/")
                Select Case i
                    Case 0
                        year = str
                    Case 1
                        month = str
                    Case 2
                        day = str
                End Select
                i = i + 1
            Next

            a.ReadXMLByID(ArticleID, day, month, year)

            Dim strUserPwd = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(context.Request.QueryString("UserPass"), "MD5")
            If a.ArticlePassword = strUserPwd Then
                util.SetCookie("Pwd_" & a.ArticlesID.ToString(), strUserPwd)
                'context.Session. .Add("Pwd_" & a.ArticlesID.ToString(), System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(context.Request.QueryString("UserPass"), "MD5"))
                context.Response.Write("OK")
            Else
                context.Response.Write("Fail")
            End If
        Catch ex As Exception
            context.Response.Write("Error")
        End Try
    End Sub

    ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class